/**
 * 
 */
package com.audi.box.server.handler;

import android.util.Log;

import com.audi.box.controller.IController;
import com.audi.box.controller.MemberController;
import com.audi.box.server.message.HandlerException;
import com.audi.dto.MemberItem;

/**
 * @author HuyPVA
 * 
 */
public class MemberHandler extends Handler {
	private static MemberHandler memberHandler;
	private static Object memberObj = new Object();

	/**
	 * @param controller
	 */
	private MemberHandler(IController controller) {
		super(controller);
	}

	public static MemberHandler getInstance(IController controller) {
		if (memberHandler == null) {
			synchronized (memberObj) {
				if (memberHandler == null) {
					memberHandler = new MemberHandler(controller);
				}
				return memberHandler;
			}
		} else {
			return memberHandler;
		}
	}

	/*
	 * Get DeviceController
	 */
	public MemberController getMemberController() {
		return (MemberController) getController();
	}

	/*
	 * Process message by action
	 */
	@Override
	public Object process(int actionId, String data) throws HandlerException {
		Object result = null;
		MemberItem memberItem = null;
		switch (actionId) {

		case ADD_MEMBER:
			memberItem = getGson().fromJson(data, MemberItem.class);
			result = getMemberController().addMember(memberItem);
			break;
		case GET_LIST_MEMBER:
			result = getMemberController().getMemberList();
			break;
		case UPDATE_MEMBER:
			memberItem = getGson().fromJson(data, MemberItem.class);
			result = getMemberController().updateMember(memberItem);
			break;
		case DELETE_MEMBER:
			memberItem = getGson().fromJson(data, MemberItem.class);
			result = getMemberController().deleteMember(memberItem);
			break;
		case CHECK_MEMBER_EXIST:
			memberItem = getGson().fromJson(data, MemberItem.class);
			result = getMemberController().isExist(memberItem);
			break;
		default:
			Log.e("AudiBox", "[MemberHandler.java] Function not support: " + actionId);
			throw new HandlerException("Function " + actionId +" not found!!!");
		}
		return result;

	}

}
